<template>
  <div class="dialog-box" style="position: relative">
    <el-scrollbar class="custom-scroll content-box">
      <el-form
        ref="formEditTeacherBasicRef"
        :model="formData"
        :size="layoutStore.defaultFormItemSize"
        :rules="rules"
        label-width="120px"
        label-position="right"
        @submit.prevent
      >
        <el-row :gutter="16">
          <el-col :span="24">
            <el-form-item label="工号" prop="TeacherBasic.gh">
              <el-input
                class="input-item"
                v-model="formData.TeacherBasic.gh"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="隶属单位号" prop="TeacherBasic.lsdwh">
              <el-input
                class="input-item"
                v-model="formData.TeacherBasic.lsdwh"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="工作单位号" prop="TeacherBasic.gzdwh">
              <el-input
                class="input-item"
                v-model="formData.TeacherBasic.gzdwh"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="进入现工作单位日期" prop="TeacherBasic.jrxgzdwrq">
              <el-date-picker
                class="input-item"
                v-model="formData.TeacherBasic.jrxgzdwrq"
                type="date"
                placeholder=""
                :clearable="true"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="编制类别码" prop="TeacherBasic.bzlbm">
              <el-input
                class="input-item"
                v-model="formData.TeacherBasic.bzlbm"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="当前状态码" prop="TeacherBasic.dqztm">
              <el-input
                class="input-item"
                v-model="formData.TeacherBasic.dqztm"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="是否上师大委培生" prop="TeacherBasic.sfssdwps">
              <el-input
                class="input-item"
                v-model="formData.TeacherBasic.sfssdwps"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength=""
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-scrollbar>
    <el-row class="footer-box" type="flex" justify="end" align="middle">
      <el-button :size="layoutStore.defaultFormItemSize" @click="onCancel()">取消</el-button>
      <el-button :size="layoutStore.defaultFormItemSize" type="primary" @click="onSubmitTeacherBasicClick()">保存</el-button>
    </el-row>
  </div>
</template>

<script lang="ts">
export default {
  name: 'formEditTeacherBasic',
};
</script>

<script setup lang="ts">
import { DialogProp } from '@/components/Dialog/types';
import * as validateRules from '@/common/utils/validate';
import { VxeColumn, VxeTable } from 'vxe-table';
import { ANY_OBJECT } from '@/types/generic';
import { DictData, DictionaryBase } from '@/common/staticDict/types';
import { ElMessage, ElMessageBox, UploadFile } from 'element-plus';
import { useRoute, useRouter } from 'vue-router';
import { useCommon } from '@/common/hooks/useCommon';
import { useLayoutStore, useStaticDictStore } from '@/store';
import { useDownload } from '@/common/hooks/useDownload';
import { useDropdown } from '@/common/hooks/useDropdown';
import { DropdownOptions, ListData } from '@/common/types/list';
import { useTable } from '@/common/hooks/useTable';
import { TableOptions } from '@/common/types/pagination';
import { useUpload } from '@/common/hooks/useUpload';
import { useUploadWidget } from '@/common/hooks/useUploadWidget';
import { DictionaryController } from '@/api/system';
import { treeDataTranslate, findItemFromList, findTreeNodePath, findTreeNode, stringCase } from '@/common/utils';
import { TeacherBasicData } from '@/api/generated/teacherBasicController';
import { TeacherBasicController } from '@/api/generated';
import DeptSelect from '@/components/DeptSelect/index.vue';
import UserSelect from '@/components/UserSelect/index.vue';

const router = useRouter();
const route = useRoute();
const layoutStore = useLayoutStore();
const { downloadFile } = useDownload();
const { getUploadHeaders, getUploadActionUrl, fileListToJson, parseUploadData, getPictureList } = useUpload();
const { 
  Delete,
  Search,
  Edit,
  Plus,
  Refresh,
  Picture,
  Dialog,
  mainContextHeight,
  clientHeight,
  checkPermCodeExist,
  parseParams,
  parseArrayParams,
  formatDateByStatsType,
  getDateRangeFilter,
} = useCommon();
// 静态字典
const { staticDict: StaticDict } = useStaticDictStore();

const props = withDefaults(
  defineProps<{
    subPage?: number | string | boolean;
    id?: ANY_OBJECT;
    saveOnSubmit?: boolean;
    rowData?: ANY_OBJECT;
    // 当使用Dialog.show弹出组件时，须定义该prop属性，以便对dialog进行回调
    dialog?: DialogProp<ANY_OBJECT[]>;
  }>(),
  {
    subPage: 0,
    id: undefined,
    saveOnSubmit: true,
    rowData: undefined,
    dialog: undefined,
  },
);

const formEditTeacherBasicRef = ref();
// 表单数据定义
type FormEditTeacherBasicData = {
  TeacherBasic: TeacherBasicData;
};
// 表单数据
const formData = reactive<FormEditTeacherBasicData>({
  TeacherBasic: {
    // ID
    id: undefined,
    // 工号
    gh: undefined,
    // 隶属单位号
    lsdwh: undefined,
    // 工作单位号
    gzdwh: undefined,
    // 进入现工作单位日期
    jrxgzdwrq: undefined,
    // 编制类别码
    bzlbm: undefined,
    // 当前状态码
    dqztm: undefined,
    // 是否上师大委培生
    sfssdwps: undefined,
    // 创建时间
    createTime: undefined,
    // 更新时间
    updateTime: undefined,
    // 创建用户
    createUserId: undefined,
    // 更新用户
    updateUserId: undefined,
  },
},
);
// 表单验证规则
const rules = reactive({
  'TeacherBasic.dqztm': [
  ],
  'TeacherBasic.sfssdwps': [
  ],
  'TeacherBasic.jrxgzdwrq': [
  ],
  'TeacherBasic.bzlbm': [
  ],
  'TeacherBasic.lsdwh': [
  ],
  'TeacherBasic.gzdwh': [
  ],
  'TeacherBasic.gh': [
  ],
});
const instance = getCurrentInstance();

const onCancel = () => {
  if (props.dialog) {
    props.dialog.cancel();
  }
};

const isEdit = computed(() => {
  return props.saveOnSubmit ? props.id != null : props.rowData != null;
});

// 初始化页面数据
const loadTeacherBasicData = () => {
  return new Promise<void>((resolve, reject) => {
    if (!isEdit.value) {
      resolve();
      return;
    }
    if (!props.saveOnSubmit && props.rowData != null) {
      formData.TeacherBasic = JSON.parse(JSON.stringify(props.rowData));
      resolve();
      return;
    }
    let params: ANY_OBJECT = {
      id: props.id
    };
    TeacherBasicController.view(params).then(res => {
      formData.TeacherBasic = { ...res.data };
      resolve();
    }).catch(e => {
      reject(e);
    });
  });
};
const onUploadError = () => {
  ElMessage.error('文件上传失败');
};
const onUploadLimit = () => {
  ElMessage.error('已经超出最大上传个数限制');
};
const refreshFormEditTeacherBasic = () => {
  // 刷新段落
};
/**
 * 重置过滤值
 */
const resetFormEditTeacherBasic = () => {
  refreshFormEditTeacherBasic();
};
/**
 * 重置所有过滤值
 */
const resetFilter = () => {
  resetFormEditTeacherBasic();
};
/**
 * 保存
 */
const onSubmitTeacherBasicClick = () => {
  formEditTeacherBasicRef.value.validate((valid) => {
    if (!valid) return;
    // 级联操作
    if (!props.saveOnSubmit) {
      let retFormData = {
        ...formData.TeacherBasic
      };
  props.dialog?.submit(retFormData);
      return;
    }
    let params: ANY_OBJECT = {
      teacherBasicDto: {
        id: formData.TeacherBasic.id,
        gh: formData.TeacherBasic.gh,
        lsdwh: formData.TeacherBasic.lsdwh,
        gzdwh: formData.TeacherBasic.gzdwh,
        jrxgzdwrq: formData.TeacherBasic.jrxgzdwrq,
        bzlbm: formData.TeacherBasic.bzlbm,
        dqztm: formData.TeacherBasic.dqztm,
        sfssdwps: formData.TeacherBasic.sfssdwps,
        createTime: formData.TeacherBasic.createTime,
        updateTime: formData.TeacherBasic.updateTime,
        createUserId: formData.TeacherBasic.createUserId,
        updateUserId: formData.TeacherBasic.updateUserId,
      }
    };

    let httpCall = isEdit.value ? TeacherBasicController.update : TeacherBasicController.add;
    httpCall(params).then(res => {
      ElMessage.success('保存成功');
      props.dialog?.submit();
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
  });
};
const formInit = () => {
  loadTeacherBasicData().then(res => {
    if (isEdit.value) refreshFormEditTeacherBasic();
  }).catch(e => {
    // TODO: 异常处理
    console.error(e);
  });
};

onMounted(() => {
  formInit();
});
</script>
